/* Do file for 'The Old Terrorsm: A Dataset, 1860--1969'
Author: Josh Tschantret
Correspondence: joshua-tschantret@uiowa.edu

Notes: Besides the terrorist groups data, the variables here are derived from 
the V-Dem 8 dataset (www.v-dem.net/). Robustness checks also include Vreeland's
XPolity data, which are easily accessed online (https://github.com/n-klotz/X-Polity-Index). 
All data tranformations are included here.

Analysis run using Stata 15. Must install firthlogit package to estimate the
penalized maximum likelihood models (ssc install firthlogit). 
*/


//Generate the cubic polynomial
xtset ccode year
by ccode (year): gen c = (_n == 1) | (L.terronset == 1)
by ccode (year): gen c_num = sum(c)
by ccode c_num (year), sort: gen t = _n
gen t2 = t*t
gen t3 = t2*t

//Other variable transformations
gen lngdp = ln(e_migdppc)
gen lnpop = ln(e_population)
gen normpolity = ((e_polity2 + 10) * 5) / 100

//Table 1 
logit terronset c.normpolity##c.normpolity e_miinterc lngdp lnpop t t2 t3, ///
	robust cluster(country)
	
outreg2 using table1.doc, replace ctitle(Model 1) addtext(Model, Logit)
	
firthlogit terronset c.normpolity##c.normpolity e_miinterc lngdp lnpop t t2 t3

outreg2 using table1.doc, append ctitle(Model 2) addtext(Model, PML)

//Figure 3
logit terronset c.normpolity##c.normpolity e_miinterc lngdp lnpop t t2 t3, ///
	robust cluster(country)
	
margins, at(normpolity=(0(0.01)1)) atmean
marginsplot, recast(line) recastci(rarea)

//Robustness checks from Appendix table 1
logit terronset c.normpolity##c.normpolity e_Civil_War lngdp lnpop t t2 t3, ///
	robust cluster(country)
outreg2 using tableA1.doc, replace ctitle(Model 3) addtext(Model, Logit)

xtlogit terronset c.normpolity##c.normpolity e_miinterc lngdp lnpop t t2 t3, re
outreg2 using tableA1.doc, append ctitle(Model 4) addtext(Model, RE)

nbreg terrcount c.normpolity##c.normpolity e_miinterc lngdp lnpop, ///
	robust cluster(country)
outreg2 using tableA1.doc, append ctitle(Model 5) addtext(Model, NB)
	
tsset ccode year
logit terronset c.normpolity##c.normpolity L.e_miinterc lngdp lnpop t t2 t3, ///
	robust cluster(country)
outreg2 using tableA1.doc, append ctitle(Model 6) addtext(Model, Logit)

//Robustness checks from Appendix table 2
logit terronset c.e_polity2##c.e_polity2 e_miinterc lngdp lnpop t t2 t3, ///
	robust cluster(country)
	
outreg2 using tableA2.doc, replace ctitle(Model 7) addtext(Model, Logit)
	
firthlogit terronset c.e_polity2##c.e_polity2 e_miinterc lngdp lnpop t t2 t3

outreg2 using tableA2.doc, append ctitle(Model 8) addtext(Model, PML)

//Robustness checks from Appendix table 3
logit terronset c.xpolity##c.xpolity e_miinterc lngdp lnpop t t2 t3, ///
	robust cluster(country)
	
outreg2 using tableA3.doc, replace ctitle(Model 9) addtext(Model, Logit)
	
firthlogit terronset c.xpolity##c.xpolity e_miinterc lngdp lnpop t t2 t3

outreg2 using tableA3.doc, append ctitle(Model 10) addtext(Model, PML)

//Robustness checks from Appendix tabel 4
logit terronset c.normpolity##c.normpolity e_Civil_War lngdp lnpop t t2 t3 wwi, ///
	robust cluster(country)
outreg2 using tableA4.doc, replace ctitle(Model 11) addtext(Model, Logit)
	
firthlogit terronset c.normpolity##c.normpolity e_Civil_War lngdp lnpop t t2 t3 wwi
outreg2 using tableA4.doc, append ctitle(Model 12) addtext(Model, PML)
	
logit terronset c.normpolity##c.normpolity e_Civil_War lngdp lnpop t t2 t3 wwii, ///
	robust cluster(country)
outreg2 using tableA4.doc, append ctitle(Model 13) addtext(Model, Logit)
	
firthlogit terronset c.normpolity##c.normpolity e_Civil_War lngdp lnpop t t2 t3 wwii
outreg2 using tableA4.doc, append ctitle(Model 14) addtext(Model, PML)

//Robustness checks from Appendix table 5
logit terronset c.normpolity##c.normpolity e_miinterc lngdp lnpop west t t2 t3, ///
	robust cluster(country)
	
outreg2 using tableA5.doc, replace ctitle(Model 15) addtext(Model, Logit)
	
firthlogit terronset c.normpolity##c.normpolity e_miinterc lngdp lnpop west t t2 t3

outreg2 using tableA5.doc, append ctitle(Model 16) addtext(Model, PML)
